<?php

namespace App\Admin\Controllers\AdminPaytype;

use App\Admin\Models\Financial\FinancialRate;
use App\Admin\Models\SiteStore;
use App\Common\Models\StoreRenewalLog;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Admin\Models\AdminPaytypeList;
use App\Admin\Validate\AdminPaytype\AdminPaytype as ValidateAdminPaytypeAdminPaytype;

class AdminPaytypeController extends Controller
{
    /**
     * 获取账户管理列表
     */
    public function getlist(Request $request)
    {
        $list = AdminPaytypeList::getList($request);
        return response()->json(['code' => '000', 'msg' => '获取成功', 'data' => $list]);
    }

    /**
     * 获取账户管理详情
     */
    public function getinfo(Request $request)
    {
        $id = $request->id ?? 0;
        $info = AdminPaytypeList::where(['id' => $id])->first();
        if (!$info) {
            return response()->json(['code' => '001', 'msg' => '参数错误']);
        }
        return response()->json(['code' => '000', 'msg' => '获取成功', 'data' => ['info' => $info]]);
    }

    /**
     * 新增账户管理
     */
    public function dopostadd(Request $request)
    {
        $req_data = $request->only(['paytype_name', 'type', 'img_path', 'bank_name', 'bank_card_no', 'bank_card_holder']);

        $Validate = new ValidateAdminPaytypeAdminPaytype;
        if (!$Validate->check($req_data)) {
            return response()->json(['code' => '001', 'msg' => $Validate->getError()]);
        }

        $isexist = AdminPaytypeList::query()
            ->where([['paytype_name', $req_data['paytype_name']],['is_del',1]])
            ->first();

        if ($isexist) {
            return response()->json(['code' => '001', 'msg' => '收款方式不允许重复']);
        }
        if ($req_data['type'] == 1) {
            if (!isset($req_data['img_path']) || $req_data['img_path'] == '') {
                return response()->json(['code' => '001', 'msg' => '收款二维码未上传']);
            }
            if (!isset($req_data['bank_card_no']) || $req_data['bank_card_no'] == '') {
                return response()->json(['code' => '001', 'msg' => '收款账户未填写']);
            }

            $Validate = new ValidateAdminPaytypeAdminPaytype('checkbank');
            if (!$Validate->check($req_data)) {
                return response()->json(['code' => '001', 'msg' => $Validate->getError()]);
            }

        } else if ($req_data['type'] == 2) {
            $Validate = new ValidateAdminPaytypeAdminPaytype('checkbank');
            if (!$Validate->check($req_data)) {
                return response()->json(['code' => '001', 'msg' => $Validate->getError()]);
            }
        }



        $data = $req_data;
        $data['create_at'] = time();
        $data['update_at'] = time();
        AdminPaytypeList::create($data);
        return response()->json(['code' => '000', 'msg' => '新增账户成功']);
    }


    public function dopostedit(Request $request)
    {
        $req_data = $request->only(['id', 'paytype_name', 'type', 'img_path', 'bank_name', 'bank_card_no', 'bank_card_holder']);


        $Validate = new ValidateAdminPaytypeAdminPaytype('edit');
        if (!$Validate->check($req_data)) {
            return response()->json(['code' => '001', 'msg' => $Validate->getError()]);
        }
        $info = AdminPaytypeList::where([['id', $req_data['id']],['is_del',1]])->first();
        if (!$info) {
            return response()->json(['code' => '001', 'msg' => '参数错误']);
        }

        $isexist = AdminPaytypeList::where([['paytype_name', $req_data['paytype_name']], ['id', '<>',$req_data['id']]])->first();

        if ($isexist) {
            return response()->json(['code' => '001', 'msg' => '收款方式不允许重复']);
        }
        if ($req_data['type'] == 1) {
            if (!isset($req_data['img_path']) || $req_data['img_path'] == '') {
                return response()->json(['code' => '001', 'msg' => '收款二维码未上传']);
            }
            if (!isset($req_data['bank_card_no']) || $req_data['bank_card_no'] == '') {
                return response()->json(['code' => '001', 'msg' => '收款账户未填写']);
            }
            $Validate = new ValidateAdminPaytypeAdminPaytype('checkbank');
            if (!$Validate->check($req_data)) {
                return response()->json(['code' => '001', 'msg' => $Validate->getError()]);
            }

        } else if ($req_data['type'] == 2){
            $Validate = new ValidateAdminPaytypeAdminPaytype('checkbank');
            if (!$Validate->check($req_data)) {
                return response()->json(['code' => '001', 'msg' => $Validate->getError()]);
            }
        }
        $data = $req_data;
        unset($data['id']);
        $data['update_at'] = time();

        AdminPaytypeList::where(['id' => $req_data['id']])->update($data);

        return response()->json(['code' => '000', 'msg' => '编辑账户成功']);
    }

    /**
     * 支付方式删除
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function payTypeDel(Request $request)
    {
        $params = $request->only(['id']);

        $rate = FinancialRate::query()
            ->where('pay_type_id',$params['id'])
            ->where('is_del',1)
            ->first();

        if(!empty($rate)){
            return response()->json(['code' => '001', 'msg' => '该支付方式在费率表使用无法删除']);
        }
        $log = StoreRenewalLog::query()
            ->where('pay_type',$params['id'])
            ->first();
        if(!empty($log)){
            return response()->json(['code' => '001', 'msg' => '该支付方式在续费管理表使用无法删除']);
        }

        // 查询支付方式是否在使用
        AdminPaytypeList::query()
            ->where(['id' => $params['id']])
            ->update(['is_del'=>2,'del_at'=>time()]);

        return response()->json(['code' => '000', 'msg' => '删除成功']);

    }
}
